﻿// ———————————————————————–
// <copyright file="Monitor.aspx.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// Monitor.aspx.cs - "Iterates Through Message Queues To Determine Priority Of Messages Inside."
// Project: "ICNETREST"- "Monitor.aspx"
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010
// Author:      Don McGarry The MITRE Corporation
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)
                
using System;
using System.Messaging;

namespace ICNETREST
{
  /// <summary>
  /// Looks at several message queues to determine which contained messages have the lowest priority. 
  /// </summary>
  public partial class Monitor : System.Web.UI.Page
  {
    /// <summary>
    /// Calculates the messages with the lowest priority in several queues.
    /// </summary>
    /// <param name="sender">The object that caused this event.</param>
    /// <param name="e">The arguments supplied.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
      this.DeadCountLabel.Text = this.CountLowestPriority(".\\private$\\deadletterservice/deadletter.svc").ToString();
      this.DEProcCountLabel.Text = this.CountLowestPriority(".\\private$\\deprocessingservice/deprocessing.svc").ToString();
      this.ExceptionCountLabel.Text = this.CountLowestPriority(".\\private$\\exceptionservice/exception.svc").ToString();
      this.ExposureCountLabel.Text = this.CountLowestPriority(".\\private$\\exposureprocessingservice/exposureprocessing.svc").ToString();
      this.FederationCountLabel.Text = this.CountLowestPriority(".\\private$\\federationservice/federation.svc").ToString();
      this.KeywordRoutingCountLabel.Text = this.CountLowestPriority(".\\private$\\keywordroutingservice/keywordrouting.svc").ToString();
      this.RouterCountLabel.Text = this.CountLowestPriority(".\\private$\\routingservice/incomingedxl").ToString();
    }

    /// <summary>
    /// Iterates through messages in a queue and examines their priorities.
    /// </summary>
    /// <param name="queueName">The queue to iterate through.</param>
    /// <returns>A 32 bit int representing the number of items in a queue.</returns>
    protected uint CountLowestPriority(string queueName)
    {
      // Holds the count of Lowest priority messages.
      uint numberItems = 0;

      // Connect to a queue.
      MessageQueue myQueue = new MessageQueue(queueName);

      // Get a cursor into the messages in the queue.
      MessageEnumerator myEnumerator =
        myQueue.GetMessageEnumerator2();

      myQueue.MessageReadPropertyFilter.AdministrationQueue = false;
      myQueue.MessageReadPropertyFilter.ArrivedTime = false;
      myQueue.MessageReadPropertyFilter.CorrelationId = false;
      myQueue.MessageReadPropertyFilter.Priority = false;
      myQueue.MessageReadPropertyFilter.ResponseQueue = false;
      myQueue.MessageReadPropertyFilter.SentTime = false;
      myQueue.MessageReadPropertyFilter.Body = false;
      myQueue.MessageReadPropertyFilter.Label = false;
      myQueue.MessageReadPropertyFilter.Id = false;

      // Move to the next message and examine its priority.
      while (myEnumerator.MoveNext())
      {
        numberItems++;
      }

      return numberItems;
    }
  }
}